import Context from "../Context";
import Wx, {wx} from "./Wx";

export enum WxEvent {
    /**
     * isConnected    boolean    当前是否有网络链接
     * networkType    string    网络类型
     *       wifi    wifi 网络
     *       2g    2g 网络
     *       3g    3g 网络
     *       4g    4g 网络
     *       unknown    Android 下不常见的网络类型
     *       none    无网络
     */
    NETWORK_CHANGED = "networkChanged",
    /**
     * value= landscape/ portrait
     */
    ORIENTATION_CHANGED = "orientationChanged",
    /**
     * level = 5/10/15
     */
    MEMORY_WARNING = "memoryWarning",
    /**
     * onWindowResize
     *      windowWidth    number    变化后的窗口宽度，单位 px
     *      windowHeight    number    变化后的窗口高度，单位 px
     */
    WINDOW_RESIZE = "windowResize",
    /**
     * window onShow
     *  scene    string    场景值
     *  query    Object    查询参数
     *  shareTicket    string    shareTicket
     *  referrerInfo    object    当场景为由从另一个小程序或公众号或App打开时，返回此字段
     */
    ON_SHOW = "onShow",

    /**
     * window onHide
     */
    ON_HIDE = "onHide",

    /**
     * window onError
     *      message    string    错误
     *      stack    string    错误调用堆栈
     */
    ON_ERROR = "onError",

    /**
     * onAudioInterruptionBegin
     * 游戏音频被系统事件中断事件.
     */
    ON_AUDIO_PAUSED = "onAudioInterruptionBegin",

    /**
     * onAudioInterruptionEnd
     * 系统中断音频事件结束.
     */
    ON_AUDIO_RESUME = "onAudioInterruptionEnd",

    /**
     * 玩家登录成功的消息.
     */
    ON_LOGIN_SUCCESS = "onLoginSuccess",
    /**
     * 修改天赋状态
     */
    ON_CHANGETALENT ="onChangeTalent",
     /**
     * 金钱改变
     */
    ON_CHANGEGOLD ="onChangeGold",
      /**
     * 锻造成功
     */
    ON_FORGINGSUCCESS ="onForgingSuccess",
}

/**
 * 微信各种事件监听适配器.
 * 微信事件将转换为框架系统事件进行分发.
 */
class WxEventAdapter {
    public setup(context: Context) {
        if (!Wx.isWeChatEnv()) {
            return;
        }
        wx.onNetworkStatusChange((res) => {
            context.messenger.sendMessage(WxEvent.NETWORK_CHANGED, [res]);
        })
        wx.onDeviceOrientationChange((res) => {
            context.messenger.sendMessage(WxEvent.ORIENTATION_CHANGED, [res]);
        })
        wx.onMemoryWarning(res => {
            context.messenger.sendMessage(WxEvent.MEMORY_WARNING, [res]);
        })
        wx.onWindowResize(res => {
            context.messenger.sendMessage(WxEvent.WINDOW_RESIZE, [res]);
        })
        wx.onShow(res => {
            context.messenger.sendMessage(WxEvent.ON_SHOW, [res]);
        });
        wx.onHide(() => {
            context.messenger.sendMessage(WxEvent.ON_HIDE);
        });
        wx.onError(res => {
            context.messenger.sendMessage(WxEvent.ON_ERROR, [res]);
        });
        wx.onAudioInterruptionBegin(() => {
            context.messenger.sendMessage(WxEvent.ON_AUDIO_PAUSED);
        });
        wx.onAudioInterruptionEnd(() => {
            context.messenger.sendMessage(WxEvent.ON_AUDIO_RESUME);
        });
    }
}

export const wxEventAdapter: WxEventAdapter = new WxEventAdapter();
